﻿<?php //action 7?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>

<script type="text/javascript" src="javascripts/jquery.js"></script>
<script type='text/javascript' src='javascripts/jquery.autocomplete.js'></script>
<link rel="stylesheet" type="text/css" href="stylesheets/jquery.autocomplete.css" />
<script type="text/javascript">
$().ready(function() {
	$("#miestas").autocomplete("includes/get_course_list.php", {
		width: 260,
		matchContains: true,
		//mustMatch: true,
		//minChars: 0,
		//multiple: true,
		//highlight: false,
		//multipleSeparator: ",",
		selectFirst: false
	});
});
</script> 
 
<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	//$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	//$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

 <?php //procesinam ka gaunam is post
 
	if (isset($_POST['miestas'])) {
		if ((($_POST['miestas']) == NULL) or (($_POST['miestas']) == "Kitas")){
		$to = "problem.php";
		redirect_to ($to);
		};
		$miestas = mysql_prep($_POST['miestas']);		
		 //o dabar bus miesto paieska =]
			
		
		
			$miestas_id = strstr($miestas, " [");
			$miestas_id = str_replace("[", "", $miestas_id);
			$miestas_id = str_replace("]", "", $miestas_id);
			//echo $miestas_id;
		
		
		
		$sql = "UPDATE `user` SET";
		$sql .= " `gyvmiestasid` =";
		$sql .= $miestas_id . " ";
		$sql .= ", `adresas` =";
		$sql .= "'" . $_POST['adresas'] . "' ";
		$sql .= " WHERE `userid` = ";
		$sql .= ($_GET['userid']);
		$sql .= " LIMIT 1;";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
			if (($_GET['new'])==1){
			$to = "darbas_iki.php?new=1&userid=" . $_GET['userid'];
		}else{
			$to = "summary.php?userid=" . $_GET['userid'];
		};	
			redirect_to($to);
				// // Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		
		
	};
	
  

 ?>



					<article>
						<h2>Gyvenamoji vieta</h2>
	<?php echo $message."</br>";?>
	<?php //pagrindiniai kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);
	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
	$adres = $subject['adresas'];
	
	//Miestas
	//query is miestai table
	$query = "SELECT *";
	$query .= " FROM `user`, `miestai`, `rajonai`, `valstybes` ";
	$query .= " WHERE";
	$query .= "`user`.`gyvmiestasid` = `miestai`.`miestaiid`";
	$query .= " AND";
	$query .= " `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND";
	$query .= " `miestai`.rajonaiid = `rajonai`.rajonaiid";
	$query .= " AND";
	$query .= " `miestai`.valstybesid = `valstybes`.valstybesid";
	
	$result_set = mysql_query($query, $connection);
	confirm_query($result_set); 
		if (!$result_set) {
			die("Database query failed: " . mysql_error());
		} else {
	$miest = mysql_fetch_array($result_set);
		};
	
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form autocomplete="off" action="adresas.php?userid=<?php echo ($_GET['userid']);if (($_GET['new'])==1){echo "&new=1";};?>" method="post" name="submit">
	<table width="330" border="0" cellspacing="2" cellpadding="3">
		<tr>
			<td>	 
			 
			 Gatvė, Namas - Butas: 
			</td>
			<td> 
			 <input name="adresas" type="text" value="<?php
										echo $adres;
										?>" />
			</td>
			<td></td>
			
		<tr>
			<td>
			 Miestas: 
			</td>
			<td>
			 <input type="text" name="miestas" id="miestas" value="<?php echo($miest['miestai']) . ", " . ($miest['rajonai']) . ", " . ($miest['valstybes']) . " [" . ($miest['miestaiid']) . "]";?>"/>
			</td>
			<td><a href="add_miestas.php">Pridėti miestą</a></td>
		</tr>
		<tr>	  
			<td>

			
			<input name="Submit" type="submit" value="<?php if (($_GET['new'])==1) {echo"Toliau";}else{echo"Redaguoti";}; ?>" />
			</td>
		</tr>
	</table>
</form>


			</article>
            
<?php require("includes/footer.php"); ?>
